var gb = {
	/**
	 * 正则
	 */
	reg : {
		chn : /[\u4E00-\u9FA5\uF900-\uFA2D]/,
		ip24 : new RegExp("^" +
		"(?!(?:127)(?:\\.\\d{1,3}){3})"
				+ "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
				+ "(?:\\.(?:[0-9]|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])){2}"
				+ "(?:\\.(?:[0-9]|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-4]))"
				+ "(\/([8-9]|[1-2][0-9]|3([0]|[2])))?" + "(-"
				+ "(?!(?:127)(?:\\.\\d{1,3}){3})"
				+ "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
				+ "(?:\\.(?:[0-9]|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])){2}"
				+ "(?:\\.(?:[0-9]|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-4]))"
				+ "(\/([8-9]|[1-2][0-9]|3([0]|[2])))?" + ")?" + "$", "i"),
		// 192.168.1.1
		IPv4 : /^([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/,
		// len : /^([0-9]|[1-2][0-9]|3[0-2])$/,
		// 8-30 或者 32
		len : /^([8-9]|[1-2][0-9]|3([0]|[2]))$/,
		positive_num : /^[1-9][0-9]*$/,
		num : /^[0-9]*$/,
		// 非负整数或小数[小数最多精确到小数点后两位]
		num_2 : /^(\d|([1-9]\d+))(\.\d{1,2})?$/,
		space : /\s/,
		// 6-15位数字字母下划线
		pass : /^[a-zA-Z0-9_]{6,32}$/,
		// 数字字母下划线
		name : /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/,
		email : /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
		tel : /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
	}

};

// 创建UUID
gb.createUUID = (function(uuidRegEx, uuidReplacer) {
	return function() {
		return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(uuidRegEx,
				uuidReplacer).toUpperCase();
	};
})(/[xy]/g, function(c) {
	var r = Math.random() * 16 | 0, v = c == "x" ? r : (r & 3 | 8);
	return v.toString(16);
});

// form serializeJSON
$.fn.serializeJSON = function() {
	var formDatas = this.serializeArray();
	var _obj = {};
	formDatas.forEach(function(_d) {
		_obj[_d.name] = _d.value;
	});
	return _obj;
};

/** ajax请求 */
gb.XHR = function(params) {
	var params = $.extend({
		// 返回类型默认json
		dataType : "json",
		// 默认发送信息至服务器时内容编码类型
		// contentType : "application/x-www-form-urlencoded",
		contentType : "application/json",
		// 请求类型默认POST
		type : "POST",
		// 默认超时时间
		timeout : 1800000,
		// 默认请求同步
		isAsync : false
	}, params);
	$.ajax({
		url : params.url,
		dataType : params.dataType,
		type : params.type,
		timeout : params.timeout,
		data : params.queryParams,
		contentType : params.contentType,
		async : params.isAsync,
		success : function(data) {
			if (params.callback) {
				params.callback(data);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

			console.log(XMLHttpRequest.responseText);
			console.log(XMLHttpRequest.readyState);
			console.log(errorThrown);
		},
		complete : function(XMLHttpRequest, status) { // 请求完成后最终执行参数
			if (params.complete) {
				params.complete(XMLHttpRequest, status);
			}
			if (status == 'timeout') {// 超时,status还有success,error等值的情况
				console.log('请求超时');
			}
		}
	});
};

/** 清空checkbox */
$.fn.clearCheckbox = function() {
	return this.each(function() {
		var $that = $(this);
		$that.attr("checked", false);
	});
}

/** 通过ajax请求获取数据并执行回调 */
gb.callbackAfterXhr = function(params) {
	var params = $.extend({
		isAsync : true
	}, params);
	gb.XHR({
		url : params.url,
		queryParams : params.queryParams,
		contentType : params.contentType,
		timeout : params.timeout,
		isAsync : params.isAsync,
		callback : function(data) {
			if (params.callback) {
				params.callback(data);
			} else {
				if (data.success) {
					params.callback(data);
				} else {
					if (!params.failCallback) {
						console.log("操作失败:" + data.message);
					} else {
						params.failCallback(data);
					}
				}
			}
		},
		complete : params.complete
	});
};

// 处理键盘事件 禁止后退键（Backspace）密码或单行、多行文本框除外
gb.banBackSpace = function(e) {
	var ev = e || window.event;// 获取event对象
	var obj = ev.target || ev.srcElement;// 获取事件源

	var t = obj.type || obj.getAttribute('type');// 获取事件源类型

	// 获取作为判断条件的事件类型
	var vReadOnly = obj.getAttribute('readonly');
	var vEnabled = obj.getAttribute('enabled');
	// 处理null值情况
	vReadOnly = (vReadOnly == null) ? false : vReadOnly;
	vEnabled = (vEnabled == null) ? true : vEnabled;

	// 当敲Backspace键时，事件源类型为密码或单行、多行文本的，
	// 并且readonly属性为true或enabled属性为false的，则退格键失效
	var flag1 = (ev.keyCode == 8
			&& (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vEnabled != true)) ? true
			: false;

	// 当敲Backspace键时，事件源类型非密码或单行、多行文本的，则退格键失效
	var flag2 = (ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea") ? true
			: false;

	// 判断
	if (flag2) {
		return false;
	}
	if (flag1) {
		return false;
	}
};
// 禁止后退键 作用于Firefox、Opera
document.onkeypress = gb.banBackSpace;
// 禁止后退键 作用于IE、Chrome
document.onkeydown = gb.banBackSpace;


gb.getCookie = function(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1
			c_end = document.cookie.indexOf(";", c_start)
			if (c_end == -1)
				c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start, c_end))
		}
	}
	return ""
}
/*这是有设定过期时间(time)的使用示例： 
s20是代表20秒 
h是指小时，如12小时则是：h12 
d是天数，30天则：d30 
setCookie("name","hayden","s20");*/
gb.setCookie = function(name, value,time) {
    var strsec = getsec(time); 
    var exp = new Date(); 
    exp.setTime(exp.getTime() + strsec*1); 
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
}

gb.delCookie = function(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = gb.getCookie(name);
	if (cval != null)
		document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
function getsec(str)
{ 
   var str1=str.substring(1,str.length)*1; 
   var str2=str.substring(0,1); 
   if (str2=="s")
   { 
        return str1*1000; 
   }
   else if (str2=="h")
   { 
       return str1*60*60*1000; 
   }
   else if (str2=="d")
   { 
       return str1*24*60*60*1000; 
   } 
} 